<template>
	<view class="order-container">
		<!-- 状态切换选项卡 -->
		<uni-segmented-control :current="currentTab" :values="['未完成', '已完成']" @clickItem="handleTabChange" />

		<!-- 订单列表 -->
		<scroll-view scroll-y class="order-list">
			<view v-for="(item,index) in orderList" :key="index" class="order-item">
				<view style="display: flex;">
					<text class="label">卡号:{{ formatCardNumber(item.kahao) }}</text>
					<text style="margin-left: 35rpx;">卡类别: {{ item.lipinka[0].leixing }}</text>
					<text style="color: darkgray;font-size: 25rpx;margin-left: 25rpx;margin-right: 5rpx;">
						用{{ item.lipinka[0].shiyong }}/余{{ item.lipinka[0].shengyu }}</text>
					<text v-if="currentTab === 0" @click="confirmOrder(item)"
						style="margin-left: auto; padding: 7rpx 20rpx;background-color:blue;border-radius: 10rpx;color: white;">
						发货
					</text>
				</view>
				<view class="address-container">
					<view class="address">
						{{ item.consignee }},{{item.mobile}}, {{ item.province }}
						{{ item.city }}{{ item.district }}{{ item.address }}
					</view>
				</view>
				<view style="display: flex;justify-content: space-between;align-items: center;">
					<view style="display: flex;flex-direction: column; color: darkgray;font-size: 25rpx;">
						<view style="color: darkgray;font-size: 25rpx;">
							订单时间: {{ item.creat_time }}
						</view>
						<view v-if="item.fahuo_time" style="color: darkgray;font-size: 25rpx;">
							发货时间: {{ item.fahuo_time }}
						</view>
					</view>
					<view class="copy-icon" @click="copyAddress(item)">
						📋
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				copyText: '',
				showToast: false,
				currentTab: 0, // 0-未完成 1-已完成
				orderList: [{
					"address": "测试一号",
					"city": "北京市",
					"cityId": "110100",
					"consignee": "高伟",
					"district": "东城区",
					"districtId": "110101",
					"lipinka": [{
						"kahao": "",
						"kazhuangtai": null,
						"youxiaoqi": "",
						"leixing": "",
						"price": null,
						"next_duihuantime": "",
						"shengyu": null,
						"shiyong": null
					}],
					"isDefault": false,
					"mobile": "18935191559",
					"province": "北京市",
					"provinceId": "110000",
					"kahao": "101033",
					"mima": "540072",
					"creat_time": "2025-05-12 17:22:08",
					"status": 0
				}],
			}
		},
		async onLoad() {
			this.loadOrderData()
			// 时间戳转格式化字符串（本地时间）
			this.formatDate = (timestamp) => {
				const date = new Date(timestamp);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份+1补零
				const day = String(date.getDate()).padStart(2, '0');
				const hours = String(date.getHours()).padStart(2, '0');
				const minutes = String(date.getMinutes()).padStart(2, '0');
				const seconds = String(date.getSeconds()).padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			};
		},
		methods: {
			copyAddress(item) {
				// 拼接需要复制的文本
				this.copyText =
					`${item.consignee},${item.mobile}, ${item.province} ${item.city}${item.district}${item.address}`;
				// 调用uniapp的复制API
				uni.setClipboardData({
					data: this.copyText,
					success: () => {
						// 复制成功提示
						this.showToast = true;
						setTimeout(() => {
							this.showToast = false;
						}, 2000);
					},
					fail: (err) => {
						console.error('复制失败:', err);
						uni.showToast({
							title: '复制失败，请重试',
							icon: 'none'
						});
					}
				});
			},
			// 格式化卡号显示
			formatCardNumber(num) {
				return num.replace(/(\d{4})\d{8}(\d{4})/, '$1 &zwnj;****&zwnj; $2')
			},

			// 切换选项卡
			handleTabChange(e) {
				this.currentTab = e.currentIndex
				this.loadOrderData()
			},

			// 调用云函数获取数据
			async loadOrderData() {
				uni.showLoading({
					title: "加载中……"
				})
				this.orderList = []
				const res = await uniCloud.callFunction({
					name: 'fun1',
					data: {
						leixing: 6,
						status: this.currentTab
					}
				})
				console.log(res);
				uni.hideLoading()
				this.orderList = res.result.data
			},

			// 确认发货操作
			async confirmOrder(item) {
				uni.showLoading({
					title: "数据更新中……"
				})
				await uniCloud.callFunction({
					name: 'fun1',
					data: {
						leixing: 7,
						tiaojian: {
							_id: item._id
						},
						shuju: {
							status: 1,
							fahuo_time:this.formatDate(new Date().getTime())
						}
					}
				})

				this.loadOrderData()
			}
		}
	}
</script>

<style scoped>
	.order-item {
		font-size: 29rpx;
		margin: 20rpx;
		padding: 20rpx 20rpx 0 20rpx;
		background: #fff;
		border-radius: 12rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
	}


	.status-tag {
		display: block;
		margin-top: 30rpx;
		color: #09bb07;
		text-align: right;
	}

	/* 6666 */
	.address-container {
		display: flex;
		align-items: center;
		padding: 10px 0 8rpx 0;
		/* margin: 10rpx 0 10rpx 0; */
		border-bottom: 1px solid #eee;
	}

	.address {
		flex: 1;
		font-size: 14px;
		color: #333;
		line-height: 20px;
	}

	.copy-icon {
		width: 40px;
		height: 40px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	/* 复制成功提示样式 */
	.toast-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(0, 0, 0, 0.3);
		z-index: 9999;
	}
</style>